package com.woniuxy.interceptor;

import com.woniuxy.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {

    //前置通知重写
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.判断会话域中是否有用户信息
        HttpSession session = request.getSession();
        //2.如果为空则表示没有登录，跳转到登录页面，并且拦截
        User user = (User) session.getAttribute("user");
        if (user == null){
            System.out.println("拦截到非法的访问：" + request.getRequestURI());
            //跳转登录页面
            response.sendRedirect("/index.html");
            return false;
        }
        //成功放行
        return true;
    }
}
